<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2018 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 老猫 <zxxjjforever@163.com>
// +----------------------------------------------------------------------
namespace app\admin\controller;

use app\admin\model\GoodsModel;
use app\admin\model\OrderModel;
use cmf\controller\AdminBaseController;
use think\Db;

class OrderController extends AdminBaseController
{


    public function index()
    {
        $this->view->g_id = $g_id = trim(input('g_id',''));
        $this->view->order_status = trim($order_status = input('order_status',''));
        $this->view->order_number = trim($order_number = input('order_number/s',''));
        $this->view->phone = $phone = trim(input('phone/s',''));
        $this->view->create_time_min = trim($create_time_min = input('create_time_min/s',''));
        $this->view->create_time_max = trim($create_time_max = input('create_time_max/s',''));

        if(request()->isAjax()){
            $data = Db::name('order')
                ->alias('order')
                ->order('order_id desc')
                ->join([config('database')['prefix'].'goods'=>'goods'],'order.g_id = goods.g_id','left')
                ->field([
                    'order.*',
                    'goods.g_name'
                ]);


            if($g_id){
                //匹配产品
                $data = $data->where('order.g_id',$g_id);
            }

            if($order_status){
                //匹配订单状态
                $data = $data->where('order.order_status',$order_status);
            }

            if($order_number){
                //匹配订单号
                $data = $data->where('order.order_number',$order_number);
            }

            if($phone){
                //匹配联系方式
                $data = $data->whereLike('order.phone',"%{$phone}%");
            }

            if($create_time_min){
                //匹配开始时间
                $data = $data->whereTime('order.create_time','>=',$create_time_min);
            }
            if($create_time_max){
                //匹配结束时间
                $data = $data->whereTime('order.create_time','<=',$create_time_max);
            }
            if($create_time_min && $create_time_max){
                //匹配时间范围
                $data = $data->whereBetween('order.create_time',[strtotime($create_time_min),strtotime($create_time_max)]);
            }

            $data = $data ->limit(input('offset/n').','.input('limit/n'))
                ->select();

            $new_data = [];
            foreach ($data as $datum) {
                $datum['create_time'] = date('Y-m-d H:i:s',$datum['create_time']);
                foreach (config('pay_all_type') as $key) {
                    if($key['alias'] == $datum['bay_type']){
                        $datum['bay_type'] = $key['name'];
                    }
                }
                $new_data[]=$datum;
            }
            return $this->success($data,'',$new_data,100);
        }
        $this->view -> goods_all = GoodsModel::all();

        $this->assign('get_all',json_encode($_GET));
        return $this->fetch('');
    }

    //补单


}